package qianting.news.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import qianting.news.common.entity.State;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 自定义异常处理
 * @author: DT
 * @date: 2021/4/19 21:51
 * @version: v1.0
 */
@ControllerAdvice
@ConditionalOnMissingClass("org.springframework.cloud.gateway.filter.GlobalFilter")
@Slf4j
public class GlobalExceptionHandler {
    
    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public State bizExceptionHandler(HttpServletRequest req, Exception e){
        e.printStackTrace();
        log.error("发生业务异常！原因是：{}",e.getMessage());
        return State.fail(e.getMessage());
    }
}
